package com.drug.controller;

import com.drug.domain.Result;
import com.drug.domain.UserR;
import com.drug.service.UserRoleService;
import com.drug.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import javax.xml.ws.Response;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Random;

@Controller
public class UserController extends HandlerInterceptorAdapter {
    @Resource
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    /**
     * 登录
     * @param map 前台传来的用户名和密码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    private Result login (@RequestParam Map map,HttpServletResponse response,HttpServletRequest request) throws UnsupportedEncodingException {
        response.setHeader("content-type","text/html;charset=UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String code2 = (String) request.getSession().getAttribute("CHECKCODE_SERVER");
        map.put("code2",code2);
        Result result = userService.login(map,request);
        return result;

    }

    /**
     * 注册
     * @param userR
     */
    @ResponseBody
    @RequestMapping("/registerUser")
    public Result registerUser(UserR userR){
        Result result = userService.registerUser(userR);
        return result;
    }
    @ResponseBody
    @RequestMapping("/findUsername")
    public Result findUsername(String username){
     return userService.findUsername(username);
    }
    /**
     * 生成验证码
     * @param response
     * @param request
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/static/checkCode")
    private void checkCode(HttpServletResponse response,HttpServletRequest request) throws IOException {
        response.setHeader("pragma","no-cache");
        response.setHeader("cache-control","no-cache");
        response.setHeader("expires","0");
        int width = 80;
        int height = 30;
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        //设置画笔颜色为灰色
        g.setColor(Color.GRAY);
        //填充图片
        g.fillRect(0,0, width,height);
        String checkCode = getCheckCode();
        //将验证码放入HttpSession中
        request.getSession().setAttribute("CHECKCODE_SERVER",checkCode);

        //设置画笔颜色为黄色
        g.setColor(Color.YELLOW);
        //设置字体的小大
        g.setFont(new Font("黑体", Font.BOLD,24));
        //向图片上写入验证码
        g.drawString(checkCode,15,25);
        ImageIO.write(image,"PNG",response.getOutputStream());
    }

    /**
     * 产生4位随机字符串
     */
    public static String getCheckCode() {
        String base = "0123456789ABCDEFGabcdefg";
        int size = base.length();
        Random r = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i=1;i<=4;i++){
            int index = r.nextInt(size);
            char c = base.charAt(index);
            sb.append(c);
        }
        return sb.toString();
    }
    /**
     * 判断用户的角色
     *
     */
    @ResponseBody
    @RequestMapping("/pdRole")
    private Result pdRole(HttpServletRequest request){
        String username = (String) request.getSession().getAttribute("CHECKCODE_USERNAME");
        Result result = userRoleService.pdRole(username);
        return result;
    }

}
